//
//  CZComposeToolbar.swift
//  weibo
//
//  Created by 丁蒙 on 16/7/3.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

//不同按钮 枚举
enum CZComposeToolbarType: Int {
    //图片
    case Picture = 10
    //@
    case Mention = 20
    //#
    case Trend = 30
    //表情
    case Emoticon = 40
    //加号
    case Add = 50
}

class CZComposeToolbar: UIView {
    //告诉外界按钮被点击
    var closure:((type: CZComposeToolbarType)->())?
    //表情键盘 按钮
    var emoticonButton: UIButton?
    
    //供外界设置
    /*
     isEmotion == true 代表系统键盘
     isEmotion == false 代表表情键盘
     */
    var isEmotion: Bool = false {
        didSet {
            if isEmotion {
                emoticonButton!.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton!.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            } else {
                emoticonButton!.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton!.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置视图
    func setupUI() {
        self.backgroundColor = RandomColor()
        //添加子控件
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtons("compose_add_background",type: .Add)
        
        //设置约束
        pictureButton.snp_makeConstraints { (make) in
            make.left.equalTo(self)
            make.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(pictureButton.snp_right)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(mentionButton.snp_right)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(trendButton.snp_right)
            make.width.equalTo(addButton)
        }
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.left.equalTo(emoticonButton!.snp_right)
            make.right.equalTo(self)
        }
        
        
        
    }
    
    // MARK: - 移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    // MARK: - 创建按钮的公共方法
    private func addChildButtons(imageName: String,type: CZComposeToolbarType)-> UIButton {
        let button: UIButton = UIButton()
        
        button.tag = type.rawValue
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: #selector(CZComposeToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        addSubview(button)
        
        return button
    }
    
    // MARK: - 监听按钮点击事件
    @objc private func buttonClick(button: UIButton) {
        closure?(type: CZComposeToolbarType(rawValue: button.tag)!)
    }
}
